import Vue from 'vue'
import VueRouter from 'vue-router'

const Home = () => import('../views/home/Home')

const Teacher = () => import('../views/teacher/Teacher')
const Education = () => import('../views/teacher/Education')
const Teaching = () => import('../views/teacher/Teaching')

const Student = () => import('../views/student/Student')
Vue.use(VueRouter)

const routes = [
  { path: '', redirect:'/home'},
  { path: '/home', component: Home },
  { path: '/teacher', component: Teacher },
  { path: '/teaching', component: Teaching},
  { path: '/education', component: Education },
  { path: '/student', component: Student },
]

// 3.创建路由对象
const router = new VueRouter({
  mode: 'history',
  routes
})

// 4.路由导航守卫
router.beforeEach((to,form,next)=>{
  // 登录页直接放行
  if(to.path === '/home' || to.path === '/teacher') return next()
  // 从sessionStroage 中获取token
  const tokenStr = window.sessionStorage.getItem("token")
  // 没有token，强制跳转
  if(!tokenStr) return next('/home')
  next()
})

// 5.导出
export default router
